from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
import dbutil
import pymainwindow

pu, _ = loadUiType('inpurchasemation.ui')

class inpurchasemationApp(QMainWindow,pu):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.confirminsertButton.clicked.connect(lambda:self.add_purchaseinformation())
        self.menu.triggered[QAction].connect(self.menuTriggered)

    def menuTriggered(self, q):
        if (q.text() == "返回上一页"):
            self.mainwindow_app = pymainwindow.MWApp()
            self.close()
            self.mainwindow_app.show()

        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return

    # 数据库处理
    def add_purchaseinformation(self):
        #获取连接
        conn = dbutil.get_conn()
        #获取cursor
        cur = conn.cursor()
        #SQL语句
        sql = "insert into purchase(进货编号,商品名称,进货数量,数量单位,进货员,供应商,应付金额,进货时间) values(%s,%s,%s,%s,%s,%s,%s,%s)"

        num = self.numEdit.text()
        goodsname = self.goodsnameEdit.text()
        quantity = self.quantityEdit.text()
        quantityunit = self.quantityunitEdit.text()
        buyer = self.buyerEdit.text()
        seller = self.sellerEdit.text()
        money = self.moneyEdit.text()
        data = self.dateEdit.text()

        #执行语句
        cur.execute(sql, (num, goodsname, quantity, quantityunit, buyer, seller, money, data ))
        #insert,update,delete必须显示提交
        conn.commit()
        #关闭资源
        dbutil.close_conn(conn, cur)
        #消息提示
        self.statusBar().showMessage('进货信息添加成功！')
        self.numEdit.clear()
        self.goodsnameEdit.clear()
        self.quantityEdit.clear()
        self.quantityunitEdit.clear()
        self.buyerEdit.clear()
        self.sellerEdit.clear()
        self.moneyEdit.clear()
        self.dateEdit.clear()


def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = inpurchasemationApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication


if __name__ == '__main__':
    main()